<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

/**
 * 模块
 * @author wangdong
 */
class SiteController extends CommonController
{
   public function index($draw=1,$start=0, $length = 10, $sort = 'id', $order = array(array('dir' => 'desc'))){
       //站点列表
      if(IS_POST){
         $dbSite = M('Site');

         $roleid = session('roleid');
         $where = '1=1';
         if($roleid!=1){
            $userid = session('userid');
            $where.=' and mgr='.$userid;
         }
         $total = $dbSite->where($where)->count();
         $list = array();
         if($total>0){
            $order = $sort . ' ' . $order[0]['dir'];
            $limit = $start . "," . $length;
            $list = $dbSite->table('__SITE__ s')->field("s.*,FROM_UNIXTIME(s.addtime, \"%Y-%m-%d %H:%i:%s\") as addtime")
                ->where($where)
                ->limit($limit)
                ->order($order)
                ->select();
         }
         $data = array(
             "draw" => $draw,
             "recordsTotal" => $total,
             "recordsFiltered" => $total,
             "data" => $list
         );
         $this->ajaxReturn($data);
      }else{
         $this->display();
      }

   }


   public function add(){
      if(IS_POST){
         $sitedb = M('Site');
         $_validate = array(
             array('mobile', 'require', '手机号码必须填写！'),
             array('name', 'require','请输入站点名称'),
             array('address', 'require','请输入站点地址'),
             array('load_num', 'require','请输入装料口数量')
         );
         if (!$sitedb->validate($_validate)->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            $this->error($sitedb->getError());
         } else {
            $data =  I('post.');
            $lonlat = explode(",",$data['lonlat']);
            unset($data['lonlat']);
            $data['longitude'] = $lonlat[0];
            $data['latitude'] = $lonlat[1];
            if(empty($data['id'])){
               unset($data['id']);
               $data['addtime'] = time();
               $state = $sitedb->add($data);
               $state ? $this->success('添加成功') : $this->error('添加失败');
            }else{
               $id= $data['id'];
               unset($data['id']);
               $state = $sitedb->where('id='.$id)->save($data);
               $state ? $this->success('修改成功') : $this->error('修改失败');
            }
         }
      }else{
         $id = I('get.id',0);
         if($id>0){
            $sitedb = M('Site');
            $site = $sitedb->where('id='.$id)->find();
            $this->assign('info',$site);
         }

         $this->display();
      }

   }


   public function getLonlat(){
      $this->address = I('get.address');
      $this->lng = I('get.lng',0);
      $this->lat = I('get.lat',0);
      $this->display();
   }

   public function search($key='',$page=1){
      $db = M('Site');
      $where = "status=0";
      if (!empty($key)) {
         $where .= ' and (name like \'%' . $key . '%\' or mobile like \'%' . $key . '%\')';
      }
      $total = $db->where($where)->count();
      $order = 'addtime desc';
      $limit = ($page-1)*30 . "," . 30;
      $list = $db->field('id,name,longitude,latitude')->where($where)->order($order)->limit($limit)->select();
      $list = empty($list) ? array() : $list;
      $data = array(
          "total_count" => $total,
          "items" => $list
      );
      $this->ajaxReturn($data);
   }


}